﻿#if SRP_URP

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

namespace GameToolkit
{
    [System.Serializable]
    public struct VolumeProfileBlend : System.IEquatable<VolumeProfileBlend>
    {
        public VolumeProfile profile;
        public float weight;

        public VolumeProfileBlend(VolumeProfile profile, float weight)
        {
            this.profile = profile;
            this.weight = weight;
        }

        public bool Equals(VolumeProfileBlend other)
        {
            return profile == other.profile && Mathf.Approximately(weight, other.weight);
        }
    }

    public static class SRPUtils
    {
        public static readonly int sh_MainTex = Shader.PropertyToID("_MainTex");

        public static void ClearSettings(this VolumeProfile profile)
        {
            if (profile == null || profile.components == null)
                return;
            for (int i = 0; i < profile.components.Count; i++)
            {
                var com = profile.components[i];
                if (com == null)
                    continue;
                com.active = false;
                com.SetAllOverridesTo(false);
            }
            profile.Reset();
        }

        public static void OverrideSettings(this VolumeProfile profile, VolumeProfileBlend blend)
        {
            if (profile == null || blend.weight <= 0 || blend.profile == null || blend.profile.components == null)
                return;
            var overrides = blend.profile.components;
            if (profile.components == null)
                profile.components = new List<VolumeComponent>();
            for (int i = 0; i < overrides.Count; i++)
            {
                var setting = overrides[i];
                if (!setting.active)
                    continue;
                var tp = setting.GetType();
                VolumeComponent baseset = null;
                for (int k = 0; k < profile.components.Count; k++)
                {
                    if (profile.components[k].GetType() == tp)
                    {
                        baseset = profile.components[k];
                        break;
                    }
                }
                if (baseset == null)
                {
                    baseset = profile.Add(tp, false);// (PostProcessEffectSettings)CreateInstance(tp);
                    baseset.hideFlags = HideFlags.HideInInspector | HideFlags.HideInHierarchy;
                    baseset.name = tp.Name;
                    baseset.active = true;
                }
                baseset.active = true;
                //var parameters = baseset.parameters;
                //for (int k = 0; k < parameters.Count; k++)
                //{
                //    var toParam = setting.parameters[k];
                //    if (toParam.overrideState)
                //        parameters[k].overrideState = true;
                //}
                setting.Override(baseset, blend.weight);
                //baseset.Override(setting, blend.weight);
            }
            profile.Reset();
        }
    }
}

#endif